//
//  WYIntroductionCell.m
//  Boss
//
//  Created by admin on 2018/6/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYIntroductionCell.h"

@implementation WYIntroductionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        
        [self createSubViews];
        
        
    }
    
    
    return self;
    
}

-(void)createSubViews
{
    YYLabel *introLabel = [YYLabel new];
    self.introLabel = introLabel;
    [self.contentView addSubview:introLabel];
    introLabel.numberOfLines = 0;
    introLabel.preferredMaxLayoutWidth =(kScreenWidth - 2 * 16);
    introLabel.left = 16;
    introLabel.width = (kScreenWidth - 2 * 16);
    introLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    introLabel.displaysAsynchronously = YES;
    introLabel.ignoreCommonProperties = YES;
    introLabel.fadeOnHighlight = NO;
    introLabel.font = [UIFont systemFontOfSize:16];
    WEAKSELF
    introLabel.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        if ([weakSelf.delegate respondsToSelector:@selector(cell:didClickInLabel:textRange:WithOutLink:)]) {
            [weakSelf.delegate cell:weakSelf didClickInLabel:(YYLabel *)containerView textRange:range WithOutLink:self.outLink];
        }
    };
    
//    introLabel.highlightTapAction(<#UIView * _Nonnull containerView#>, <#NSAttributedString * _Nonnull text#>, <#NSRange range#>, <#CGRect rect#>)
    [introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-16);
    }];
    
    
//    UIButton * loctionBtn = [[UIButton alloc]init];
//   // [self.contentView addSubview:loctionBtn];
//    [loctionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(introLabel.mas_bottom).offset(15);
//        make.left.mas_equalTo(16);
//        make.bottom.mas_equalTo(-15);
//    }];
//    [loctionBtn setTitle:@"北京市通州区北运河西" forState:UIControlStateNormal];
//    [loctionBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    loctionBtn.titleLabel.font = [UIFont systemFontOfSize:13];
//    
    
    
    

}

-(void)setOutLink:(NSString *)outLink
{
    _outLink = outLink;
}

-(void)setType:(NSString *)type
{
    _type = type;
    
    if (self.outLink.length) {
        //  YYTextContainer  *titleContarer = [YYTextContainer new];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:type];
//        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, attributedString.length)];
        
        
        [attributedString yy_setFont:[UIFont systemFontOfSize:16] range:NSMakeRange(0, attributedString.length)];
        
        UIImage *image = [UIImage imageNamed:@"link_icon"];
        NSAttributedString *blueVText = [NSMutableAttributedString yy_attachmentStringWithEmojiImage:image fontSize:16];
        [attributedString yy_appendString:@" "];
        [attributedString appendAttributedString:blueVText];
        [attributedString yy_appendString:@"网页链接"];
        NSString *string = @"网页链接";
        NSRange searchRange = NSMakeRange(0, attributedString.string.length);
        
        NSRange imageRange = [attributedString.string rangeOfString:blueVText.string options:kNilOptions range:searchRange];
        if (imageRange.location!=NSNotFound) {
            
            [attributedString yy_setColor:[UIColor hexStringToColor:@"#527ead" alpha:1.0] range:imageRange];
        }
        
        
        
        NSRange range = [attributedString.string rangeOfString:string options:kNilOptions range:searchRange];
        if (range.location == NSNotFound) return;
        if ([attributedString yy_attribute:YYTextHighlightAttributeName atIndex:range.location] == nil) {
            [attributedString yy_setColor:[UIColor hexStringToColor:@"#527ead" alpha:1.0] range:range];
            [attributedString yy_setFont:[UIFont systemFontOfSize:16] range:range];
            // 高亮状态
            YYTextHighlight *highlight = [YYTextHighlight new];
            //[highlight setBackgroundBorder:highlightBorder];
            // 数据信息，用于稍后用户点击
            //  highlight.userInfo = @{kWBLinkTopicName : topic};
            [attributedString yy_setTextHighlight:highlight range:range];
        }
       
        [attributedString addAttributes:@{NSKernAttributeName:@(0.8)} range:NSMakeRange(0, attributedString.length)];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:8];
       // [paragraphStyle setParagraphSpacing:8];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [type length])];
        self.introLabel.attributedText = attributedString;
    }else{
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:type attributes:@{NSKernAttributeName:@(0.8)}];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:4];
       // [paragraphStyle setFirstLineHeadIndent:15.0];
       // [paragraphStyle setParagraphSpacing:5];
        //[paragraphStyle setMinimumLineHeight:0.0];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [type length])];
        [attributedString yy_setFont:[UIFont systemFontOfSize:16] range:NSMakeRange(0, attributedString.length)];
        
        self.introLabel.attributedText = attributedString;
    
       // self.introLabel.text = type;
    }
    
  
    
    
    //attributedString at
    
    
    
}


@end
